package org.realteam.hibernate.test.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;

import org.hibernate.ejb.Ejb3Configuration;
import org.realteam.hibernate.test.model.Message;

public class JPAEntityManager {
	
	public static void main(String[] args) {
		
		Ejb3Configuration cfg = new Ejb3Configuration();
		
		EntityManagerFactory entityManagerFactory = cfg.configure("hibernate.cfg.xml")
				.addAnnotatedClass(Message.class).buildEntityManagerFactory();
		
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		
		Message message = new Message();
		message.setText("My second message");
		
		Message nextMsg = new Message();
		nextMsg.setText("My second next message");
		 
		message.setNextMessage(nextMsg);
		
		entityManager.persist(message);
		transaction.commit();
		entityManager.close();
	}
}
